package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.domain.TbServicePackageAssociation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.system.mapper.TbServicePackageMapper;
import com.ruoyi.system.domain.TbServicePackage;
import com.ruoyi.system.service.ITbServicePackageService;

/**
 * 服务套餐管理Service业务层处理
 * 
 * @author hkj
 * @date 2024-09-10
 */
@Service
public class TbServicePackageServiceImpl implements ITbServicePackageService 
{
    @Autowired
    private TbServicePackageMapper tbServicePackageMapper;

    /**
     * 查询服务套餐管理
     * 
     * @param packageId 服务套餐管理主键
     * @return 服务套餐管理
     */
    @Override
    public TbServicePackage selectTbServicePackageByPackageId(Long packageId)
    {
        return tbServicePackageMapper.selectTbServicePackageByPackageId(packageId);
    }

    /**
     * 查询服务套餐管理列表
     * 
     * @param tbServicePackage 服务套餐管理
     * @return 服务套餐管理
     */
    @Override
    public List<TbServicePackage> selectTbServicePackageList(TbServicePackage tbServicePackage)
    {
        return tbServicePackageMapper.selectTbServicePackageList(tbServicePackage);
    }

    /**
     * 新增服务套餐管理
     * 
     * @param tbServicePackage 服务套餐管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTbServicePackage(TbServicePackage tbServicePackage)
    {
        tbServicePackage.setCreateTime(DateUtils.getNowDate());
        tbServicePackage.setCreateBy(SecurityUtils.getUsername());
        int rows = tbServicePackageMapper.insertTbServicePackage(tbServicePackage);
        insertTbServicePackageAssociation(tbServicePackage);
        return rows;
    }

    /**
     * 修改服务套餐管理
     * 
     * @param tbServicePackage 服务套餐管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTbServicePackage(TbServicePackage tbServicePackage)
    {
        tbServicePackage.setUpdateTime(DateUtils.getNowDate());
        tbServicePackage.setUpdateBy(SecurityUtils.getUsername());
        tbServicePackageMapper.deleteTbServicePackageAssociationByPackageId(tbServicePackage.getPackageId());
        insertTbServicePackageAssociation(tbServicePackage);
        return tbServicePackageMapper.updateTbServicePackage(tbServicePackage);
    }

    /**
     * 批量删除服务套餐管理
     * 
     * @param packageIds 需要删除的服务套餐管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbServicePackageByPackageIds(Long[] packageIds)
    {
        tbServicePackageMapper.deleteTbServicePackageAssociationByPackageIds(packageIds);
        return tbServicePackageMapper.deleteTbServicePackageByPackageIds(packageIds);
    }

    /**
     * 删除服务套餐管理信息
     * 
     * @param packageId 服务套餐管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbServicePackageByPackageId(Long packageId)
    {
        tbServicePackageMapper.deleteTbServicePackageAssociationByPackageId(packageId);
        return tbServicePackageMapper.deleteTbServicePackageByPackageId(packageId);
    }

    /**
     * 新增服务-套餐关联信息
     * 
     * @param tbServicePackage 服务套餐管理对象
     */
    public void insertTbServicePackageAssociation(TbServicePackage tbServicePackage)
    {
        List<TbServicePackageAssociation> tbServicePackageAssociationList = tbServicePackage.getTbServicePackageAssociationList();
        Long packageId = tbServicePackage.getPackageId();
        if (StringUtils.isNotNull(tbServicePackageAssociationList))
        {
            List<TbServicePackageAssociation> list = new ArrayList<TbServicePackageAssociation>();
            for (TbServicePackageAssociation tbServicePackageAssociation : tbServicePackageAssociationList)
            {
                tbServicePackageAssociation.setPackageId(packageId);
                tbServicePackageAssociation.setUpdateTime(DateUtils.getNowDate());
                tbServicePackageAssociation.setUpdateBy(SecurityUtils.getUsername());
                list.add(tbServicePackageAssociation);
            }
            if (list.size() > 0)
            {

                tbServicePackageMapper.batchTbServicePackageAssociation(list);
            }
        }
    }
}
